package com.hwd.gmall.list.controller;

import com.hwd.gmall.common.result.Result;
import com.hwd.gmall.list.service.GoodsService;
import com.hwd.gmall.model.list.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 商品相关的控制层
 *
 * @author 黄伟东/Victor
 * @date 2022/4/27 15:37
 */
@RestController
@RequestMapping(value = "/list/goods")
public class GoodsController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private GoodsService goodsService;

    /**
     * 创建索引、创建映射
     *
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @GetMapping
    public Result<Object> createIndexAndMapping() {
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * 向es中保存数据，商品上架时调用
     *
     * @param skuId 商品id
     * @return boolean
     */
    @GetMapping(value = "/saveDataIntoEs/{skuId}")
    public boolean saveDataIntoEs(@PathVariable(value = "skuId") Long skuId) {
        goodsService.saveSkuIntoEs(skuId);
        return true;
    }

    /**
     * 在es中删除数据，商品下架时调用
     *
     * @param skuId 商品id
     * @return boolean
     */
    @GetMapping(value = "/removeDataFromEs/{skuId}")
    public boolean removeDataFromEs(@PathVariable(value = "skuId") Long skuId) {
        goodsService.removeGoodsFromEs(skuId);
        return true;
    }

    /**
     * 给指定商品添加热度值
     *
     * @param goodsId 商品id
     * @return boolean
     */
    @GetMapping(value = "/addScore/{goodsId}")
    public boolean addScore(@PathVariable(value = "goodsId") Long goodsId) {
        goodsService.addScore(goodsId);
        return true;
    }
}
